#include <cstdio>
#include <queue>
#include <vector>

/**
 * Luogu P1195: Connecting Clouds (to create marshmallows)
 */

struct Edge {
    int x, y;
    int cost;

    bool operator>(const Edge &other) const { return cost > other.cost; }
};

std::vector<int> root;

int find(int v) {
    if (root[v] != v) root[v] = find(root[v]);
    return root[v];
}

void union_(int u, int v) {
    int r1 = find(u), r2 = find(v);
    root[r2] = r1;
}

int main() {
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    // Input
    int nVerts, nEdges, nComps;
    scanf("%d %d %d", &nVerts, &nEdges, &nComps);
    for (int i = 0; i < nVerts; i++) root.push_back(i);
    std::priority_queue<Edge, std::vector<Edge>, std::greater<Edge> > heap;
    for (int i = 0; i < nEdges; i++) {
        Edge e;
        scanf("%d %d %d", &e.x, &e.y, &e.cost);
        heap.push(e);
    }

    // Create minimal spanning forest
    int nConnect = nVerts - nComps;
    int count = 0, cost = 0;
    while (count < nConnect) {
        auto e = heap.top();
        heap.pop();
        if (find(e.x) == find(e.y)) continue;
        union_(e.x, e.y);
        cost += e.cost;
        count++;
    }
    printf("%d\n", cost);

    return 0;
}
